package world0.menu;

import org.lwjgl.opengl.Display;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.openal.Audio;
import org.newdawn.slick.openal.AudioLoader;
import org.newdawn.slick.openal.SoundStore;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.util.ResourceLoader;

import world0.World0Game;
import world0.menu.Button;

public class MainMenuState extends BasicGameState {
	
	int stateID = -1;
	
	/* Images */
	Image background;
	Image buttonActive;
	
	int buttonActiveX;
	int buttonActiveY;
	
	/* Buttons */
	Button startGame;
	Button saveGame;
	Button loadGame;
	Button options;
	Button exit;
	
	/* Audio */
	Audio buttonOver;
	Audio buttonPressed;
	Audio backgroundLoop;
	
	boolean buttonSoundPlay = false;
	boolean backgroundSoundPlay = false;
	
	/* State methodes */
	public MainMenuState (int stateID) {
		this.stateID = stateID;
	}
	
	@Override
	public int getID() {
		return stateID;
	}
	
	public void init (GameContainer gc, StateBasedGame sbg) throws SlickException
	{
		/* Curser icon */
		Image curser = new Image("textures/icons/curser.png");
		gc.setMouseCursor(curser, 0, 0);
		
		/* simple images */
		background = new Image("textures/menu/bg_center.jpg");
		
		/* Buttons(X, Y, Image, AudioOver, AudioPressed, GameContainer) */
		int distanceY = 300 + ((gc.getHeight() / 100) * 3); // first button
		int distanceYValue = 70; // distance lines
		
		startGame = new Button((gc.getWidth() / 2), distanceY, "textures/menu/startgame.png", "sounds/menu/buttonOver.ogg", "sounds/menu/buttonPressed.ogg", gc);
		saveGame = new Button((gc.getWidth() / 2), (distanceY += distanceYValue), "textures/menu/savegame.png", "sounds/menu/buttonOver.ogg", "sounds/menu/buttonPressed.ogg", gc);
		loadGame = new Button((gc.getWidth() / 2), (distanceY += distanceYValue), "textures/menu/loadgame.png", "sounds/menu/buttonOver.ogg", "sounds/menu/buttonPressed.ogg", gc);
		options = new Button((gc.getWidth() / 2), (distanceY += distanceYValue), "textures/menu/options.png", "sounds/menu/buttonOver.ogg", "sounds/menu/buttonPressed.ogg", gc);
		exit = new Button((gc.getWidth() / 2), (distanceY += distanceYValue), "textures/menu/exit.png", "sounds/menu/buttonOver.ogg", "sounds/menu/error.ogg", gc);
		
		
		/* Button active effect */
		buttonActive = new Image("textures/menu/active.png");
		buttonActiveX = gc.getWidth() /2;
		buttonActiveY = 300 + ((gc.getHeight() / 100) * 3);
		
		/* Background music */
		try {
			backgroundLoop = AudioLoader.getAudio("OGG", ResourceLoader.getResourceAsStream("sounds/menu/introloop.ogg"));
		} catch (Throwable e) { e.printStackTrace();}
	}
	
	public void render (GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
		
		/* Images */
		background.draw(gc.getWidth() / 2 - background.getWidth() /2, 0);
		
		/* Button active effect */
		buttonActive.drawCentered(buttonActiveX, buttonActiveY);
		
		/* Buttons */
		startGame.draw();
		loadGame.draw();
		saveGame.draw();
		options.draw();
		exit.draw();
	}
	
	public void update (GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
		
		/* Over effect */
		if(startGame.buttonAction(gc)) {setActivePosition(startGame);}
		if(saveGame.buttonAction(gc)) {setActivePosition(saveGame);}
		if(loadGame.buttonAction(gc)) {setActivePosition(loadGame);}
		if(options.buttonAction(gc)) {setActivePosition(options);}
		if(exit.buttonAction(gc)) {setActivePosition(exit);}
		
		if(!backgroundSoundPlay) {
			backgroundSoundPlay = true;
			backgroundLoop.playAsMusic(0.8f, 1.0f, true);
		}
		
		/* StartGame button */
		if(startGame.buttonPressed(gc)) {
			setActivePosition(startGame);
			backgroundLoop.stop();
			backgroundSoundPlay = false;
			//gc.setMouseGrabbed(true);
			sbg.enterState(World0Game.GAMEPLAYSTATE);
		}

		/* Exit button */
		if(exit.buttonPressed(gc)) {
			setActivePosition(startGame);
			exit.buttonPlayButtonPressed();
			try {Thread.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}
			Display.destroy();
			System.exit(0);
		}
		SoundStore.get().poll(0);
	}
	
	public void setActivePosition(Button button) {
		buttonActiveY = button.getPositionY();
	}
}